import 'package:flutter/material.dart';
// import '../home.dart';
// import '../favorite.dart';
import 'package:url_launcher/url_launcher.dart';
import '../ads/consent_manager.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:flutter/cupertino.dart';
import 'package:in_app_review/in_app_review.dart';
import 'package:share_plus/share_plus.dart';

class CustomDrawer extends StatefulWidget {
  const CustomDrawer({
    super.key
  });

  @override
  _CustomDrawerState createState() => _CustomDrawerState();
}

class _CustomDrawerState extends State<CustomDrawer> {
  var _isPrivacyOptionsRequired = false;
  final InAppReview inAppReview = InAppReview.instance;

  void _getIsPrivacyOptionsRequired() async {
    if (await ConsentManager.instance.isPrivacyOptionsRequired()) {
      setState(() {
        _isPrivacyOptionsRequired = true;
      });
    }
  }

  void _launchURL(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'not to : $url';
    }
  }

  @override
  void initState() {
    super.initState();

    ConsentManager.instance.gatherConsent((consentGatheringError) {
      if (consentGatheringError != null) {
        // Consent not obtained in current session.
        debugPrint(
            "${consentGatheringError.errorCode}: ${consentGatheringError.message}");
      }

      // Check if a privacy options entry point is required.
      _getIsPrivacyOptionsRequired();

      // Attempt to initialize the Mobile Ads SDK.
    });

  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      backgroundColor: Colors.white,
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
           const DrawerHeader(
            decoration: BoxDecoration(
              color: Color.fromARGB(255, 215, 31, 39),
              image: DecorationImage(image: AssetImage('images/images.webp'), fit: BoxFit.cover),
            ),
            child: Column(
              children: [
                // Container(
                //   width: 120,
                //   height: 120,
                //   decoration: const BoxDecoration(
                //     borderRadius: BorderRadius.all(Radius.circular(16)),
                //     image: DecorationImage(
                //       image: AssetImage('images/app-logo.jpg'), // 替换为你的图片路径
                //       fit: BoxFit.cover, // 图片填充方式
                //     ),
                //   ),
                // ),
              ],
            ),
          ),
          // ListTile(
          //   leading: const Icon(CupertinoIcons.home),
          //   title: const Text('Good Morning'),
          //   onTap: () {
          //     Navigator.push(
          //       context,
          //       MaterialPageRoute(
          //         builder: (context) => const HomePage(),
          //       ),
          //     );
          //   },
          // ),
          ListTile(
            leading: const Icon(CupertinoIcons.star),
            title: const Text('Califícanos', style: TextStyle(fontSize: 18),),
            onTap: () {
              inAppReview.openStoreListing(appStoreId: '6740426686');
            },
          ),
          ListTile(
            leading: const Icon(CupertinoIcons.person_2),
            title: const Text('Invitar a amigos', style: TextStyle(fontSize: 18)),
            onTap: () {
              Share.share('Descarga Citas de Buenos Días desde https://apps.apple.com/app/id6740426686');
            },
          ),
          ListTile(
            leading: const Icon(CupertinoIcons.lock),
            title: const Text('Política de privacidad', style: TextStyle(fontSize: 18)),
            onTap: () {
              _launchURL('https://www.tomatocx.com/privacy-policy.html');
            },
          ),

          if(_isPrivacyOptionsRequired )
            ListTile(
              leading: const Icon(CupertinoIcons.settings),
              title: const Text('Configuración de Privacidad', style: TextStyle(fontSize: 18)),
              onTap: () {
                ConsentForm.showPrivacyOptionsForm((formError) {
                  if (formError != null) {
                    debugPrint(
                        "${formError.errorCode}: ${formError.message}");
                  }
                });
              },
            ),
        ],
      ),
    );
  }
}
